package com.weixin.paleituo.adapter;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.weixin.paleituo.R;
import com.weixin.paleituo.base.BaseRecycleAdapter;
import com.weixin.paleituo.bean.CouponsBean;
import com.weixin.paleituo.help.RecycleViewHolder;
import com.weixin.paleituo.util.StringUtils;
import com.weixin.paleituo.util.UIUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author yichao
 * Time  2018/2/24 13:47
 * Dest  优惠券
 */

public class CouponsAdapter extends BaseRecycleAdapter<CouponsBean> {

    private LinearLayout mLlExplain;
    private Map<Integer, String> mMap = new HashMap<>();
    private TextView mTvExplain;
    private TextView mTvSymbo;
    private TextView mTvMoney;
    private TextView mTvName;
    private TextView mTvTitle;
    private TextView mTvtime;

    private LinearLayout mLlOne,mLlTwo ;
    private TextView mTvCouponSum;


    public CouponsAdapter(Context context, List<CouponsBean> datas) {
        super(context, datas, R.layout.item_coupons);
    }

    @Override
    protected void setData(final RecycleViewHolder holder, CouponsBean couponsBean, final int position) {
        mLlExplain = holder.getItemView(R.id.ll_explain_item_coupons);
        mTvExplain = holder.getItemView(R.id.tv_explain_item_coupons);
        mTvSymbo = holder.getItemView(R.id.tv_symbol_item_coupons);
        mTvMoney = holder.getItemView(R.id.tv_money_item_coupons);
        mTvName = holder.getItemView(R.id.tv_name_item_coupons);
        mTvTitle = holder.getItemView(R.id.tv_title_item_coupons);
        mTvtime = holder.getItemView(R.id.tv_time_item_coupons);
        final ImageView mImageArrow;
        mImageArrow = holder.getItemView(R.id.iv_arrow_item_coupons);
        mLlOne = holder.getItemView(R.id.ll_one);
        mLlTwo = holder.getItemView(R.id.ll_two);
        mTvCouponSum = holder.getItemView(R.id.tv_coupons_sum_item_coupons);

        mTvTitle.setText(couponsBean.getCouponName() + "");
        mTvExplain.setText(couponsBean.getIllustrate() + "");

        mTvName.setText(couponsBean.getCompany() + "");
        mTvCouponSum.setText("券额："+couponsBean.getMoney());

            int state = couponsBean.getState();
             int type = couponsBean.getType();
        //-未使用2-已使用3-已过期（默认传1）
            if (state == 1) {
                //未使用
                if (type==1){
                    mTvSymbo.setTextColor(UIUtils.getColor(R.color.c_ffff313f));
                    mTvMoney.setTextColor(UIUtils.getColor(R.color.c_ffff313f));
                    mTvName.setTextColor(UIUtils.getColor(R.color.c_ffff313f));
                    mTvCouponSum.setTextColor(UIUtils.getColor(R.color.c_ffff313f));

                }else if (type==2){

                    mTvSymbo.setTextColor(UIUtils.getColor(R.color.c_29CE7E));
                    mTvMoney.setTextColor(UIUtils.getColor(R.color.c_29CE7E));
                    mTvName.setTextColor(UIUtils.getColor(R.color.c_29CE7E));
                    mTvCouponSum.setTextColor(UIUtils.getColor(R.color.c_29CE7E));

                }
                mTvMoney.setText(couponsBean.getBanlance()+"");

                mTvTitle.setTextColor(UIUtils.getColor(R.color.c_ff383838));
                mTvtime.setTextColor(UIUtils.getColor(R.color.c_ff383838));
             //   mTvExplain.setBackgroundColor(UIUtils.getColor(R.color.c_ffffffff));
                mTvExplain.setBackgroundResource(R.drawable.coupons_unuse_circle_shape);
                mLlOne.setBackgroundResource(R.mipmap.bg_w1);
                mLlTwo.setBackgroundResource(R.mipmap.bg_w2);


            } else if (state == 2) {
                //2-已使用
                mTvSymbo.setTextColor(UIUtils.getColor(R.color.c_ffffffff));
                mTvMoney.setTextColor(UIUtils.getColor(R.color.c_ffffffff));
                mTvName.setTextColor(UIUtils.getColor(R.color.c_ff383838));
                mTvCouponSum.setTextColor(UIUtils.getColor(R.color.c_ffffffff));
                mTvTitle.setTextColor(UIUtils.getColor(R.color.c_ff383838));
                mTvtime.setTextColor(UIUtils.getColor(R.color.c_ff383838));
                mLlOne.setBackgroundResource(R.mipmap.invalid_bg1);
                mLlTwo.setBackgroundResource(R.mipmap.used_bg2);
             //   mTvExplain.setBackgroundColor(UIUtils.getColor(R.color.c_dedede));
                mTvExplain.setBackgroundResource(R.drawable.coupons_used_circle_shape);
                mTvMoney.setText(couponsBean.getUseMoney()+"");

            } else if (state == 3) {
                //3-已过期

                mTvSymbo.setTextColor(UIUtils.getColor(R.color.c_ffffffff));
                mTvMoney.setTextColor(UIUtils.getColor(R.color.c_ffffffff));
                mTvName.setTextColor(UIUtils.getColor(R.color.c_ff383838));
                mTvCouponSum.setTextColor(UIUtils.getColor(R.color.c_ffffffff));
                mTvTitle.setTextColor(UIUtils.getColor(R.color.c_ff383838));
                mTvtime.setTextColor(UIUtils.getColor(R.color.c_ff383838));
                mLlOne.setBackgroundResource(R.mipmap.invalid_bg1);
                mLlTwo.setBackgroundResource(R.mipmap.invalid_bg2);
               // mTvExplain.setBackgroundColor(UIUtils.getColor(R.color.c_dedede));
                mTvExplain.setBackgroundResource(R.drawable.coupons_used_circle_shape);
                mTvMoney.setText(couponsBean.getBanlance()+"");

            }


            String startTime = couponsBean.getStartTime();

        String endTime = couponsBean.getEndTime();

        if (!StringUtils.isEmpty(endTime)){
            String endTimess = couponsBean.getEndTime();
            mTvtime.setText("使用时间:" + startTime + "至" + endTimess);
        }else {
            mTvtime.setText("使用时间:" +"永久");
        }

        String s2 = mMap.get(position);
        if (s2 != null) {
            if (s2.equals("1")) {
                mTvExplain.setVisibility(View.VISIBLE);
            mImageArrow.setImageResource(R.mipmap.up_g_coupons);
            }
        } else {
            mTvExplain.setVisibility(View.GONE);
            mImageArrow.setImageResource(R.mipmap.down_g_coupons);
        }

        holder.mItemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                TextView tvExplain = holder.getItemView(R.id.tv_explain_item_coupons);

                String s1 = mMap.get(position);

                if (s1 != null) {
                    if (s1.equals("1")) {
                        tvExplain.setVisibility(View.GONE);
                        mMap.remove(position);
                        mImageArrow.setImageResource(R.mipmap.down_g_coupons);
                    }
                } else {
                    mMap.put(position, "1");
                    tvExplain.setVisibility(View.VISIBLE);
                    mImageArrow.setImageResource(R.mipmap.up_g_coupons);
                }
            }
        });

        mLlExplain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView tvExplain = holder.getItemView(R.id.tv_explain_item_coupons);

                String s1 = mMap.get(position);

                if (s1 != null) {
                    if (s1.equals("1")) {
                        tvExplain.setVisibility(View.GONE);
                        mMap.remove(position);
                        mImageArrow.setImageResource(R.mipmap.down_g_coupons);
                    }
                } else {
                    mMap.put(position, "1");
                    tvExplain.setVisibility(View.VISIBLE);
                    mImageArrow.setImageResource(R.mipmap.up_g_coupons);
                }
            }
        });
    }

    /**
     * 清空打开
     */
    public  void  clearMap(){
        mMap.clear();
    }
}
